<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../../css/bootstrap.min.css">
    <link rel="stylesheet" href="../../css/fontawesome/css/all.min.css">
    <script src="../../css/fontawesome/js/all.min.js"></script>
</head>

<body>
    <h2>编辑用户</h2>

    <div class="mx-5">
        <form name="form">
            <div class="mb-3">
                <label class="form-label">用户名</label>
                <input type="text" name="username" class="form-control">
            </div>
            <div class="mb-3">
                <label class="form-label">姓名</label>
                <input type="text" name="name" class="form-control">
            </div>
            <div class="mb-3">
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" checked name="gender" value="1">
                    <label class="form-check-label">
                        男
                    </label>
                </div>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" name="gender" value="2">
                    <label class="form-check-label">
                        女
                    </label>
                </div>
            </div>
            <div class="mb-3">
                <label class="form-label">年龄</label>
                <input type="number" name="age" class="form-control">
            </div>
            <div class="mb-3">
                <label class="form-label">邮箱</label>
                <input type="text" name="email" class="form-control">
            </div>
            <div class="mb-3">
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="emailVisibility">
                    <label class="form-check-label">
                        是否显示邮箱
                    </label>
                </div>
            </div>

            <div class="mb-3">
                <label class="form-label">手机</label>
                <input type="text" name="phone" class="form-control">
            </div>

            <div class="mb-3">
                <button type="button" data-action="save" class="btn btn-primary">保存</button>
                <button type="button" class="btn btn-link" onclick="location.href = './users.html'">返回</button>
            </div>
        </form>
    </div>

    <script type="module">
        import PocketBase from '../../js/pocketbase.es.js';
        import _on from '../../js/_on.js';
        import _confirm from '../../js/_confirm.js';
        import _reUrl from '../../js/_reUrl.js';
        const html = String.raw;
        const pb = new PocketBase('http://127.0.0.1:8090');
        _reUrl();

        // 表单对象
        const form = document.form;
        // 获取需要编辑的ID
        const search = new URLSearchParams(location.search);
        const id = search.get('id');

        // 回填用户信息
        (async () => {
            // 根据ID获取用户信息
            const record = await pb.collection('users').getOne(id);
            form.username.value = record.username;
            form.age.value = record.age;
            form.email.value = record.email;
            form.phone.value = record.phone;
            form.gender.value = record.gender;
            form.name.value = record.name;
            form.emailVisibility.checked = record.emailVisibility;
        })();

        _on('click', {
            async save() {
                try {
                    const record = await pb.
                        collection('users').update(id, {
                            "username": form.username.value,
                            "emailVisibility": form.emailVisibility.checked,
                            "name": form.name.value,
                            "phone": form.phone.value,
                            "gender": form.gender.value,
                            "age": form.age.value,
                        });
                    location.href = './users.html';
                    // history.back();
                    // history.forward();
                    // location.reload();
                } catch (error) {
                    console.log(error);
                }
            }

        })


    </script>
</body>


</html>